{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}

module Language.Dependencies.Adaptors.Java where

-- container
import Data.Tree (Tree(Node,rootLabel))

-- local imports
import Language.Dependencies.Language as L

import Language.Dependencies.Term2tree (data2tree)

import Data.ByteString.Char8(pack)

import Language.Java.Parser
import Generics.MultiRec.Languages.Java as J

import Language.Java.Parser.AST

import Data.Maybe


languagejava = Language 
  { L.name         = "Java" 
  , syntax       = "Java" 
  , exts         = [".java"] 
  , parse        = parJava
  , toTree       = (data2tree TheProgram :: Program -> Tree String)
  , srcLoc       = toSrcLoc
  , adjustSrcLoc = Nothing
  , srcLoctype   = (Coordinate undefined undefined)
  , typerep = Just TheProgram
  }

    
parJava :: String -> Either Error Program
parJava s = Right $ (java . alexScanTokens) s

toSrcLoc :: Tree String -> [L.Loc]
toSrcLoc (Node "SrcSpan" [ l1, l2 ]  ) = return $ Coordinate (fst $ extract l1, snd $ extract l1) (fst $ extract l2, snd $ extract l2) 
toSrcLoc _ = []

extract (Node (label) cs ) = ((read l :: Int),(read c :: Int))where
                        [cons,offset,l,c] = words label
